<?php
namespace ImiDemo\HttpDemo\MainServer\Controller;

use Imi\Db\Db;
use Imi\Util\Random;
use Imi\Bean\BeanFactory;
use Imi\Util\ObjectArrayHelper;
use Imi\Controller\HttpController;
use Imi\Server\View\Annotation\View;
use Imi\Server\Route\Annotation\Route;
use Imi\Server\Route\Annotation\Action;
use Imi\Server\Route\Annotation\Controller;
use ImiDemo\HttpDemo\MainServer\Model\Team;
use ImiDemo\HttpDemo\MainServer\Model\Test;
use ImiDemo\HttpDemo\MainServer\Model\User;
use ImiDemo\HttpDemo\MainServer\Model\UserSns;
use ImiDemo\HttpDemo\MainServer\Model\UserFriend;
use ImiDemo\HttpDemo\MainServer\Model\UserWithFriend;
use ImiDemo\HttpDemo\MainServer\Model\Article;
use ImiDemo\HttpDemo\MainServer\Model\Comment;

/**
 * Model操作 演示
 * @Controller("/ModelPolymorphicO2M/")
 * @View(renderType="json")
 */
class ModelPolymorphicO2MController extends HttpController
{
    /**
     * Model操作 演示
     * 访问：http://127.0.0.1:8080/ModelPolymorphicO2M/findArticle
     * 
     * @Action
     * @return void
     */
    public function findArticle()
    {
        $article = Article::find(1);
        return [
            'article'        =>    $article,
        ];
    }

    /**
     * Model操作 演示
     * 访问：http://127.0.0.1:8080/ModelPolymorphicO2M/select
     * 
     * @Action
     * @return void
     */
    public function select()
    {
        $list = Article::select();

        return $list;
    }

    /**
     * Model操作 演示
     * 访问：http://127.0.0.1:8080/ModelPolymorphicO2M/newInstance
     * 
     * @Action
     * @return void
     */
    public function newInstance()
    {
        $article = Article::newInstance();

        return $article;
    }

    /**
     * Model操作 演示
     * 访问：http://127.0.0.1:8080/ModelPolymorphicO2M/insert
     * 
     * @Action
     * @return void
     */
    public function insert()
    {
        $article = Article::newInstance();
        $article->title = '123';
        $article->content = 'text';
        $article->comments->append(
            Comment::newInstance(['content'=>'1']),
            Comment::newInstance(['content'=>'2'])
        );
        $result = $article->insert();

        return [
            'success'    =>    $result->getLastInsertId() > 0,
            'data'        =>    $article,
        ];
    }

    /**
     * Model操作 演示
     * 访问：http://127.0.0.1:8080/ModelPolymorphicO2M/update
     * 
     * @Action
     * @return void
     */
    public function update()
    {
        $maxId = Article::query()->max('id');
        $article = Article::find($maxId);

        $article->comments->clear();

        $result = $article->update();

        return [
            'success'    =>    $result->isSuccess(),
            'data'        =>    $article,
        ];
    }

    /**
     * Model操作 演示
     * 访问：http://127.0.0.1:8080/ModelPolymorphicO2M/delete
     * 
     * @Action
     * @return void
     */
    public function delete()
    {
        $maxId = Article::query()->max('id');
        $article = Article::find($maxId);
        $result = $article->delete();

        return [
            'success'    =>    $result->isSuccess(),
        ];
    }
}